<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book django项目 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>使用Django开发REST 接口 | django项目</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../01-day/restful/task.html" />
    
    
    <link rel="prev" href="../../01-day/restful/prepare.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="1.1.5"
        data-chapter-title="使用Django开发REST 接口"
        data-filepath="01-day/restful/django.md"
        data-basepath="../.."
        data-revision="Sun May 10 2020 14:49:33 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Django REST framework
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="01-day.html">
            
                
                    <a href="../../01-day.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Django REST framework流程
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="01-day/restful.html">
            
                
                    <a href="../../01-day/restful.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        RESTfull
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="01-day/restful/mode.html">
            
                
                    <a href="../../01-day/restful/mode.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.1.</b>
                        
                        Web应用模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="01-day/restful/restful.html">
            
                
                    <a href="../../01-day/restful/restful.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.2.</b>
                        
                        认识RESTful
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="01-day/restful/method.html">
            
                
                    <a href="../../01-day/restful/method.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.3.</b>
                        
                        RESTful设计思想
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.1" data-path="01-day/restful/method/request.html">
            
                
                    <a href="../../01-day/restful/method/request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.3.1.</b>
                        
                        请求相关
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.3.2" data-path="01-day/restful/method/response.html">
            
                
                    <a href="../../01-day/restful/method/response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.3.2.</b>
                        
                        响应相关
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.4" data-path="01-day/restful/prepare.html">
            
                
                    <a href="../../01-day/restful/prepare.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.4.</b>
                        
                        项目准备
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1.1.5" data-path="01-day/restful/django.html">
            
                
                    <a href="../../01-day/restful/django.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.5.</b>
                        
                        使用Django开发REST 接口
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.1.6" data-path="01-day/restful/task.html">
            
                
                    <a href="../../01-day/restful/task.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.6.</b>
                        
                        明确REST接口开发的核心任务
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01-day/restful/djangorestful-framework.html">
            
                
                    <a href="../../01-day/restful/djangorestful-framework.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Django REST framework
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="01-day/restful/djangorestful-framework/config.html">
            
                
                    <a href="../../01-day/restful/djangorestful-framework/config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.1.</b>
                        
                        环境安装与配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="01-day/restful/djangorestful-framework/like.html">
            
                
                    <a href="../../01-day/restful/djangorestful-framework/like.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.2.</b>
                        
                        Django REST framework魅力
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="02-day.html">
            
                
                    <a href="../../02-day.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        序列化和反序列化
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="02-day/modify.html">
            
                
                    <a href="../../02-day/modify.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Serializer字段和选项
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="02-day/serializer.html">
            
                
                    <a href="../../02-day/serializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        序列化使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.1" data-path="02-day/serializer/base.html">
            
                
                    <a href="../../02-day/serializer/base.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.1.</b>
                        
                        基本使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2.2" data-path="02-day/serializer/assoc.html">
            
                
                    <a href="../../02-day/serializer/assoc.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.2.</b>
                        
                        关联对象嵌套序列化
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="02-day/un-serializer.html">
            
                
                    <a href="../../02-day/un-serializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        反序列化使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1" data-path="02-day/un-serializer/validate.html">
            
                
                    <a href="../../02-day/un-serializer/validate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.</b>
                        
                        验证
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1.1" data-path="02-day/un-serializer/validate/type.html">
            
                
                    <a href="../../02-day/un-serializer/validate/type.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.1.</b>
                        
                        字段类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3.1.2" data-path="02-day/un-serializer/validate/field.html">
            
                
                    <a href="../../02-day/un-serializer/validate/field.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.2.</b>
                        
                        字段选项
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3.1.3" data-path="02-day/un-serializer/validate/validatefieldname.html">
            
                
                    <a href="../../02-day/un-serializer/validate/validatefieldname.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.3.</b>
                        
                        validate\_fieldname
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3.1.4" data-path="02-day/un-serializer/validate/validate.html">
            
                
                    <a href="../../02-day/un-serializer/validate/validate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.1.4.</b>
                        
                        validate
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3.2" data-path="02-day/un-serializer/save.html">
            
                
                    <a href="../../02-day/un-serializer/save.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.2.</b>
                        
                        保存
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="02-day/modelserializer.html">
            
                
                    <a href="../../02-day/modelserializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        模型类序列化器ModelSerializer
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="03-day/view.html">
            
                
                    <a href="../../03-day/view.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="03-day/view/requestresponse.html">
            
                
                    <a href="../../03-day/view/requestresponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Request 与 Response
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="03-day/view/requestresponse/request.html">
            
                
                    <a href="../../03-day/view/requestresponse/request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.1.</b>
                        
                        Request
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="03-day/view/requestresponse/response.html">
            
                
                    <a href="../../03-day/view/requestresponse/response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.2.</b>
                        
                        Response
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="03-day/view/view.html">
            
                
                    <a href="../../03-day/view/view.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="03-day/view/view/apiview.html">
            
                
                    <a href="../../03-day/view/view/apiview.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.1.</b>
                        
                        APIView\(一级\)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2.2" data-path="03-day/view/view/genericapiview.html">
            
                
                    <a href="../../03-day/view/view/genericapiview.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.2.</b>
                        
                        GenericAPIView\(二级\)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2.3" data-path="03-day/view/view/mixin.html">
            
                
                    <a href="../../03-day/view/view/mixin.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.3.</b>
                        
                        Mixin
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2.4" data-path="03-day/view/view/subclass.html">
            
                
                    <a href="../../03-day/view/view/subclass.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.4.</b>
                        
                        子类视图\(三级\)
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="03-day/view/viewset.html">
            
                
                    <a href="../../03-day/view/viewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        视图集ViewSet
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="03-day/view/viewset/viewset.html">
            
                
                    <a href="../../03-day/view/viewset/viewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        ViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="03-day/view/viewset/genericviewset.html">
            
                
                    <a href="../../03-day/view/viewset/genericviewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        GenericViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="03-day/view/viewset/modelviewset.html">
            
                
                    <a href="../../03-day/view/viewset/modelviewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        ModelViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="03-day/view/viewset/readonlymodelviewset.html">
            
                
                    <a href="../../03-day/view/viewset/readonlymodelviewset.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.4.</b>
                        
                        ReadOnlyModelViewSet
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="03-day/view/router.html">
            
                
                    <a href="../../03-day/view/router.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        路由Router
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="03-day/other.html">
            
                
                    <a href="../../03-day/other.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        高级功能
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="03-day/other/authentication.html">
            
                
                    <a href="../../03-day/other/authentication.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        认证Authentication
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="03-day/other/quan-xian-permissions.html">
            
                
                    <a href="../../03-day/other/quan-xian-permissions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        权限Permissions
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="03-day/other/throttling.html">
            
                
                    <a href="../../03-day/other/throttling.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        限流Throttling
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="03-day/other/filtering.html">
            
                
                    <a href="../../03-day/other/filtering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        过滤Filtering
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="03-day/other/order.html">
            
                
                    <a href="../../03-day/other/order.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        排序OrderingFilter
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="03-day/other/pagination.html">
            
                
                    <a href="../../03-day/other/pagination.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        分页Pagination
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="03-day/other/exceptions.html">
            
                
                    <a href="../../03-day/other/exceptions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        异常处理 Exceptions
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >django项目</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x4F7F;&#x7528;django&#x5F00;&#x53D1;rest-&#x63A5;&#x53E3;">&#x4F7F;&#x7528;Django&#x5F00;&#x53D1;REST &#x63A5;&#x53E3; </h1>
<p>&#x6211;&#x4EEC;&#x4EE5;&#x5728;Django&#x6846;&#x67B6;&#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x56FE;&#x4E66;&#x82F1;&#x96C4;&#x6848;&#x4F8B;&#x6765;&#x5199;&#x4E00;&#x5957;&#x652F;&#x6301;&#x56FE;&#x4E66;&#x6570;&#x636E;&#x589E;&#x5220;&#x6539;&#x67E5;&#x7684;REST API&#x63A5;&#x53E3;&#xFF0C;&#x6765;&#x7406;&#x89E3;REST API&#x7684;&#x5F00;&#x53D1;&#x3002;</p>
<p>&#x5728;&#x6B64;&#x6848;&#x4F8B;&#x4E2D;&#xFF0C;&#x524D;&#x540E;&#x7AEF;&#x5747;&#x53D1;&#x9001;JSON&#x683C;&#x5F0F;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-py"><span class="hljs-comment"># views.py</span>

<span class="hljs-keyword">from</span> django.views.generic <span class="hljs-keyword">import</span> View
<span class="hljs-keyword">from</span> book.models <span class="hljs-keyword">import</span> BookInfo
<span class="hljs-keyword">from</span> django.http <span class="hljs-keyword">import</span> JsonResponse,HttpResponse
<span class="hljs-keyword">import</span> json

<span class="hljs-comment"># Create your views here.</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookListView</span><span class="hljs-params">(View)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x67E5;&#x8BE2;&#x6240;&#x6709;&#x56FE;&#x4E66;&#x3001;&#x589E;&#x52A0;&#x56FE;&#x4E66;
    &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x67E5;&#x8BE2;&#x6240;&#x6709;&#x56FE;&#x4E66;
        &#x8DEF;&#x7531;&#xFF1A;GET /books/
        &quot;&quot;&quot;</span>
        queryset = BookInfo.objects.all()
        book_list = []
        <span class="hljs-keyword">for</span> book <span class="hljs-keyword">in</span> queryset:
            book_list.append({
                <span class="hljs-string">&apos;id&apos;</span>: book.id,
                <span class="hljs-string">&apos;name&apos;</span>: book.name,
                <span class="hljs-string">&apos;pub_date&apos;</span>: book.pub_date
            })
        <span class="hljs-keyword">return</span> JsonResponse(book_list, safe=<span class="hljs-keyword">False</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post</span><span class="hljs-params">(self, request)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x65B0;&#x589E;&#x56FE;&#x4E66;
        &#x8DEF;&#x7531;&#xFF1A;POST /books/
        &quot;&quot;&quot;</span>
        json_bytes = request.body
        json_str = json_bytes.decode()
        book_dict = json.loads(json_str)

        <span class="hljs-comment"># &#x6B64;&#x5904;&#x8BE6;&#x7EC6;&#x7684;&#x6821;&#x9A8C;&#x53C2;&#x6570;&#x7701;&#x7565;</span>

        book = BookInfo.objects.create(
            name=book_dict.get(<span class="hljs-string">&apos;name&apos;</span>),
            pub_date=book_dict.get(<span class="hljs-string">&apos;pub_date&apos;</span>)
        )

        <span class="hljs-keyword">return</span> JsonResponse({
            <span class="hljs-string">&apos;id&apos;</span>: book.id,
            <span class="hljs-string">&apos;name&apos;</span>: book.name,
            <span class="hljs-string">&apos;pub_date&apos;</span>: book.pub_date
        },safe=<span class="hljs-keyword">False</span>)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookDetailView</span><span class="hljs-params">(View)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x83B7;&#x53D6;&#x5355;&#x4E2A;&#x56FE;&#x4E66;&#x4FE1;&#x606F;
    &#x4FEE;&#x6539;&#x56FE;&#x4E66;&#x4FE1;&#x606F;
    &#x5220;&#x9664;&#x56FE;&#x4E66;
    &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request, id)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x83B7;&#x53D6;&#x5355;&#x4E2A;&#x56FE;&#x4E66;&#x4FE1;&#x606F;
        &#x8DEF;&#x7531;&#xFF1A; GET  /books/&lt;pk&gt;/
        &quot;&quot;&quot;</span>
        <span class="hljs-keyword">try</span>:
            book = BookInfo.objects.get(id=id)
        <span class="hljs-keyword">except</span> BookInfo.DoesNotExist:
            <span class="hljs-keyword">return</span> HttpResponse(status=<span class="hljs-number">404</span>)

        <span class="hljs-keyword">return</span> JsonResponse({
            <span class="hljs-string">&apos;id&apos;</span>: book.id,
            <span class="hljs-string">&apos;name&apos;</span>: book.name,
            <span class="hljs-string">&apos;pub_date&apos;</span>: book.pub_date
        })

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">put</span><span class="hljs-params">(self, request, id)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x4FEE;&#x6539;&#x56FE;&#x4E66;&#x4FE1;&#x606F;
        &#x8DEF;&#x7531;&#xFF1A; PUT  /books/&lt;pk&gt;
        &quot;&quot;&quot;</span>
        <span class="hljs-keyword">try</span>:
            book = BookInfo.objects.get(id=id)
        <span class="hljs-keyword">except</span> BookInfo.DoesNotExist:
            <span class="hljs-keyword">return</span> HttpResponse(status=<span class="hljs-number">404</span>)

        json_bytes = request.body
        json_str = json_bytes.decode()
        book_dict = json.loads(json_str)

        <span class="hljs-comment"># &#x6B64;&#x5904;&#x8BE6;&#x7EC6;&#x7684;&#x6821;&#x9A8C;&#x53C2;&#x6570;&#x7701;&#x7565;</span>

        book.name = book_dict.get(<span class="hljs-string">&apos;name&apos;</span>)
        book.pub_date = book_dict.get(<span class="hljs-string">&apos;pub_date&apos;</span>)
        book.save()

        <span class="hljs-keyword">return</span> JsonResponse({
            <span class="hljs-string">&apos;id&apos;</span>: book.id,
            <span class="hljs-string">&apos;name&apos;</span>: book.name,
            <span class="hljs-string">&apos;pub_date&apos;</span>: book.pub_date
        })

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete</span><span class="hljs-params">(self, request, id)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x5220;&#x9664;&#x56FE;&#x4E66;
        &#x8DEF;&#x7531;&#xFF1A; DELETE /books/&lt;pk&gt;/
        &quot;&quot;&quot;</span>
        <span class="hljs-keyword">try</span>:
            book = BookInfo.objects.get(id=id)
        <span class="hljs-keyword">except</span> BookInfo.DoesNotExist:
            <span class="hljs-keyword">return</span> HttpResponse(status=<span class="hljs-number">404</span>)

        book.delete()

        <span class="hljs-keyword">return</span> HttpResponse(status=<span class="hljs-number">204</span>)
</code></pre>
<pre><code class="lang-py">urlpatterns = [
    re_path(<span class="hljs-string">r&apos;^books/$&apos;</span>,views.BookListView.as_view()),
    re_path(<span class="hljs-string">r&apos;^books/(?P&lt;id&gt;\d+)$&apos;</span>,views.BookDetailView.as_view()),
]
</code></pre>
<h4 id="&#x6D4B;&#x8BD5;">&#x6D4B;&#x8BD5; </h4>
<p>&#x4F7F;&#x7528;Postman&#x6D4B;&#x8BD5;&#x4E0A;&#x8FF0;&#x63A5;&#x53E3;</p>
<p>1&#xFF09; &#x83B7;&#x53D6;&#x6240;&#x6709;&#x56FE;&#x4E66;&#x6570;&#x636E;</p>
<p>GET &#x65B9;&#x5F0F;&#x8BBF;&#x95EE;<a href="http://127.0.0.1:8000/books/&#xFF0C;" target="_blank">http://127.0.0.1:8000/books/&#xFF0C;</a>&#x8FD4;&#x56DE;&#x72B6;&#x6001;&#x7801;200&#xFF0C;&#x6570;&#x636E;&#x5982;&#x4E0B;</p>
<pre><code>[
    {
        &quot;id&quot;: 2,
        &quot;commentcount&quot;: 40,
        &quot;pub_date&quot;: &quot;1986-07-24&quot;,
        &quot;readcount&quot;: 36,
        &quot;name&quot;: &quot;&#x5929;&#x9F99;&#x516B;&#x90E8;&quot;,
        &quot;image&quot;: &quot;&quot;
    },
    {
        &quot;id&quot;: 3,
        &quot;commentcount&quot;: 18,
        &quot;pub_date&quot;: &quot;1995-12-24&quot;,
        &quot;readcount&quot;: 28,
        &quot;name&quot;: &quot;&#x7B11;&#x50B2;&#x6C5F;&#x6E56;&quot;,
        &quot;image&quot;: &quot;&quot;
    },
    {
        &quot;id&quot;: 4,
        &quot;commentcount&quot;: 24,
        &quot;pub_date&quot;: &quot;1987-11-11&quot;,
        &quot;readcount&quot;: 58,
        &quot;name&quot;: &quot;&#x96EA;&#x5C71;&#x98DE;&#x72D0;&quot;,
        &quot;image&quot;: &quot;&quot;
    },
    {
        &quot;id&quot;: 5,
        &quot;commentcount&quot;: 0,
        &quot;pub_date&quot;: &quot;2000-05-01&quot;,
        &quot;readcount&quot;: 0,
        &quot;name&quot;: &quot;&#x65B0;&#x5C04;&#x96D5;&#x82F1;&#x96C4;&#x4F20;&quot;,
        &quot;image&quot;: &quot;&quot;
    },
    {
        &quot;id&quot;: 6,
        &quot;commentcount&quot;: 0,
        &quot;pub_date&quot;: &quot;1990-05-01&quot;,
        &quot;readcount&quot;: 0,
        &quot;name&quot;: &quot;&#x5C04;&#x96D5;&#x82F1;&#x96C4;&#x4F20;&quot;,
        &quot;image&quot;: &quot;&quot;
    }
]
</code></pre><p>2&#xFF09;&#x83B7;&#x53D6;&#x5355;&#x4E00;&#x56FE;&#x4E66;&#x6570;&#x636E;</p>
<p>GET &#x8BBF;&#x95EE;<a href="http://127.0.0.1:8000/books/5/" target="_blank">http://127.0.0.1:8000/books/2/</a>&#xFF0C;&#x8FD4;&#x56DE;&#x72B6;&#x6001;&#x7801;200&#xFF0C; &#x6570;&#x636E;&#x5982;&#x4E0B;</p>
<pre><code>{
    &quot;id&quot;: 2,
    &quot;commentcount&quot;: 40,
    &quot;pub_date&quot;: &quot;1986-07-24&quot;,
    &quot;readcount&quot;: 36,
    &quot;name&quot;: &quot;&#x5929;&#x9F99;&#x516B;&#x90E8;&quot;,
    &quot;image&quot;: &quot;&quot;
}
</code></pre><p>GET &#x8BBF;&#x95EE;<a href="http://127.0.0.1:8000/books/100/&#xFF0C;&#x8FD4;&#x56DE;&#x72B6;&#x6001;&#x7801;404" target="_blank">http://127.0.0.1:8000/books/100/&#xFF0C;&#x8FD4;&#x56DE;&#x72B6;&#x6001;&#x7801;404</a></p>
<p>3&#xFF09;&#x65B0;&#x589E;&#x56FE;&#x4E66;&#x6570;&#x636E;</p>
<p>POST &#x8BBF;&#x95EE;<a href="http://127.0.0.1:8000/books/&#xFF0C;&#x53D1;&#x9001;JSON&#x6570;&#x636E;&#xFF1A;" target="_blank">http://127.0.0.1:8000/books/ </a>,&#x53D1;&#x9001;JSON&#x6570;&#x636E;</p>
<pre><code>{

    &quot;pub_date&quot;: &quot;1990-05-01&quot;,
    &quot;name&quot;: &quot;python&#x5165;&#x95E8;&quot;
}
</code></pre><p>&#x8FD4;&#x56DE;&#x72B6;&#x6001;&#x7801;201&#xFF0C;&#x6570;&#x636E;&#x5982;&#x4E0B;</p>
<pre><code>{
    &quot;id&quot;: 7,
    &quot;commentcount&quot;: 0,
    &quot;pub_date&quot;: &quot;1990-05-01&quot;,
    &quot;readcount&quot;: 0,
    &quot;name&quot;: &quot;python&#x5165;&#x95E8;&quot;,
    &quot;image&quot;: &quot;&quot;
}
</code></pre><p>4&#xFF09;&#x4FEE;&#x6539;&#x56FE;&#x4E66;&#x6570;&#x636E;</p>
<p>PUT &#x8BBF;&#x95EE;<a href="http://127.0.0.1:8000/books/8/&#xFF0C;&#x53D1;&#x9001;JSON&#x6570;&#x636E;&#xFF1A;" target="_blank">http://127.0.0.1:8000/books/7/</a>&#xFF0C;&#x53D1;&#x9001;JSON&#x6570;&#x636E;</p>
<pre><code>{

    &quot;pub_date&quot;: &quot;1990-05-01&quot;,
    &quot;name&quot;: &quot;python&#x9AD8;&#x7EA7;&quot;
}
</code></pre><p>&#x8FD4;&#x56DE;&#x72B6;&#x6001;&#x7801;200&#xFF0C;&#x6570;&#x636E;&#x5982;&#x4E0B;</p>
<pre><code>{
    &quot;id&quot;: 7,
    &quot;commentcount&quot;: 0,
    &quot;pub_date&quot;: &quot;1990-05-01&quot;,
    &quot;readcount&quot;: 0,
    &quot;name&quot;: &quot;python&#x9AD8;&#x7EA7;&quot;,
    &quot;image&quot;: &quot;&quot;
}
</code></pre><p>5&#xFF09;&#x5220;&#x9664;&#x56FE;&#x4E66;&#x6570;&#x636E;</p>
<p>DELETE &#x8BBF;&#x95EE;<a href="http://127.0.0.1:8000/books/8/&#xFF0C;&#x8FD4;&#x56DE;204&#x72B6;&#x6001;&#x7801;" target="_blank">http://127.0.0.1:8000/books/7/</a>&#xFF0C;&#x8FD4;&#x56DE;204&#x72B6;&#x6001;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../01-day/restful/prepare.html" class="navigation navigation-prev " aria-label="Previous page: 项目准备"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../01-day/restful/task.html" class="navigation navigation-next " aria-label="Next page: 明确REST接口开发的核心任务"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/fontsettings.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontsettings":{"family":"sans","size":2,"theme":"white"},"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book django项目 -->
</html>
